package com.begye.smartloan.apptest.huankuan

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.begye.smartloan.apptest.MyApplication
import com.begye.smartloan.apptest.R
import com.begye.smartloan.apptest.ext.toThousandUnitFormat
import com.begye.smartloan.apptest.tool.IApiProcessCallback
import com.begye.smartloan.apptest.tool.doPostRequest
import org.json.JSONObject

class HuanKuanViewModel : ViewModel() {

    var pinOrderId: String = ""
    var pinThirdOrderId: String = ""

    private val _loadingStateLiveData = MutableLiveData<Boolean>()
    val onLoadingStateLiveData get() = _loadingStateLiveData

    private val _huanKuanUrlLiveData = MutableLiveData<JSONObject>()
    val onHuanKuanUrlLiveData get() = _huanKuanUrlLiveData

    private val _pinStatusLiveData = MutableLiveData<JSONObject>()
    val onPinStatusLiveData get() = _pinStatusLiveData

    private val _huanKuanListLiveData = MutableLiveData<List<JSONObject>>()
    val onHuanKuanListLiveData get() = _huanKuanListLiveData

    private val _huanKuanDetailLiveData = MutableLiveData<JSONObject>()
    val onHuanKuanDetailLiveData get() = _huanKuanDetailLiveData

    private val _remainDaysLiveData = MutableLiveData<String>()
    val onRemainDaysLiveData get() = _remainDaysLiveData

    fun requestHuanKuanList() {
        doPostRequest(
            path = "/BZMHMybXSNAA1GB3Vsr7g",
            action = hashMapOf(),
            loading = _loadingStateLiveData,
            callback = object : IApiProcessCallback {
                override fun onApiReady() {
                }

                override fun onApiDone(jsonObject: JSONObject) {
                    try {
                        val obj = jsonObject.optJSONObject("obj") ?: JSONObject()

                        val list = mutableListOf<JSONObject>()
                        val item1 = JSONObject().apply {
                            put(
                                "showName",
                                MyApplication.context.getString(R.string.huan_kuan_text_27)
                            )
                            put(
                                "showDetail",
                                obj.optString("repayAmount", "").toThousandUnitFormat()
                            )
                            put("title", "YES")
                        }
                        list.add(item1)

                        val item2 = JSONObject().apply {
                            put(
                                "showName",
                                MyApplication.context.getString(R.string.huan_kuan_text_28)
                            )
                            put("showDetail", obj.optString("repayDate", ""))
                            put("title", "YES")
                        }
                        list.add(item2)

                        val item3 = JSONObject().apply {
                            put(
                                "showName",
                                MyApplication.context.getString(R.string.huan_kuan_text_29)
                            )
                            put("showDetail", obj.optString("amount", "").toThousandUnitFormat())
                            put("title", "NO")
                        }
                        list.add(item3)

                        val item4 = JSONObject().apply {
                            put(
                                "showName",
                                MyApplication.context.getString(R.string.huan_kuan_text_30)
                            )
                            put("showDetail", obj.optString("interest", "").toThousandUnitFormat())
                            put("title", "NO")
                        }
                        list.add(item4)

                        val item5 = JSONObject().apply {
                            put(
                                "showName",
                                MyApplication.context.getString(R.string.huan_kuan_text_31)
                            )
                            put("showDetail", obj.optString("repayProFee", ""))
                            put("title", "NO")
                        }
                        list.add(item5)

                        val item6 = JSONObject().apply {
                            val detailText = MyApplication.context.getString(
                                R.string.huan_kuan_text_34,
                                obj.optString("overdueDay", "")
                            )
                            put(
                                "showName",
                                MyApplication.context.getString(R.string.huan_kuan_text_32)
                            )
                            put("showDetail", detailText)
                            put("title", "NO")
                        }
                        list.add(item6)

                        val item7 = JSONObject().apply {
                            put(
                                "showName",
                                MyApplication.context.getString(R.string.huan_kuan_text_33)
                            )
                            put(
                                "showDetail",
                                obj.optString("overdueAmount", "").toThousandUnitFormat()
                            )
                            put("title", "NO")
                        }
                        list.add(item7)
                        _huanKuanListLiveData.value = list

                        _huanKuanDetailLiveData.value = obj

                        val remainDay = obj.optString("remainDay", "0")
                        _remainDaysLiveData.value = remainDay

                    } catch (e: Exception) {
                        //
                    }
                }

                override fun onApiError(code: String, errMsg: String) {
                }

            })
    }

    fun requestHuanKuanUrl() {
        doPostRequest(
            path = "/y7Fk0GvISYNcOk2QrhLoKg",
            action = hashMapOf(),
            loading = _loadingStateLiveData,
            callback = object : IApiProcessCallback {
                override fun onApiReady() {
                }

                override fun onApiDone(jsonObject: JSONObject) {
                    try {
                        val obj = jsonObject.optJSONObject("obj") ?: JSONObject()

                        pinThirdOrderId = obj.optString("thirdOrderId", "")
                        pinOrderId = obj.optString("orderId", "")

                        val purl = obj.optString("repayUrl", "")
                        val url = obj.optString("url", "")

                        if (purl.isBlank() and url.isBlank() and pinThirdOrderId.isNotBlank()) {
                            obj.put("isPin", true)
                        } else {
                            obj.put("isPin", false)
                        }

                        obj.put("targetUrl", purl)
                        if (purl.isBlank()) {
                            obj.put("targetUrl", url)
                        }

                        _huanKuanUrlLiveData.value = obj

                    } catch (e: Exception) {
                        //
                    }
                }

                override fun onApiError(code: String, errMsg: String) {
                }

            })
    }

    fun requestPinStatus() {
        val params = hashMapOf<String, Any>().apply {
            put("orderId", pinOrderId)
            put("thirdOrderId", pinThirdOrderId)
        }
        doPostRequest(
            path = "/kgdyh121dRFfsDdOeNTXqQ",
            action = params,
            loading = _loadingStateLiveData,
            callback = object : IApiProcessCallback {
                override fun onApiReady() {
                }

                override fun onApiDone(jsonObject: JSONObject) {
                    try {
                        val obj = jsonObject.optJSONObject("obj") ?: JSONObject()
                        _pinStatusLiveData.value = obj

                    } catch (e: Exception) {
                        //
                    }
                }

                override fun onApiError(code: String, errMsg: String) {
                }

            })
    }
}